package com.demo;

import com.demo.pagers.Helper;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/7/7.
 */
public class BasicTestCase {

     static AppiumDriver<WebElement> driver;
    private Finder finder;
    protected Helper helper;

    @BeforeMethod
    public void setUp() throws Exception {

        // set up appium   以下内容必须设置
        //获取当前项目的根路径     拼接一个app路径
        File classpathRoot = new File(System.getProperty("user.dir"));
        //当前apk路径
        File app = new File(classpathRoot, "apk/wangyixinwen_696.apk");
        //设置属性
        DesiredCapabilities capabilities = new DesiredCapabilities();
        //设备名
        capabilities.setCapability("deviceName","Android Emulator");
        //制定被测对象的路径  getAbsolutePath获得被测对象的绝对路径
        capabilities.setCapability("app", app.getAbsolutePath());
        //设置 启动类  apk包名字 com.netease.nr.biz.ad.AdActivity
        capabilities.setCapability("appActivity", "com.netease.nr.biz.ad.AdActivity");
        //启动 应用不会每次都重置
        capabilities.setCapability("noReset",true);
        //设置 输入框可以输入 中文
        capabilities.setCapability("unicodeKeyboard",true);
        capabilities.setCapability("resetKeyboard",true);
        //给driver对象 实例化
        driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        finder = new Finder(driver);
        helper = new Helper(finder);
    }

    @Test
    public void testName() throws Exception {

    }

    @AfterMethod
    public void tearDown() throws Exception {
        driver.quit();

    }

    public static void captureScreenShot(ITestResult result){
        //getMethod().getRealClass().getSimpleName() 获得 不带包路径的名字
        //result.getMethod().getMethodName()获得 方法名
        String passfailMethod = result.getMethod().getRealClass().getSimpleName()+"."+result.getMethod().getMethodName();
        //TO capture screenshot  截图
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        //构造了 时间的格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd-ss");
        //构建当前目录
        String destDir = "screenshots/Failures";
        //mkdirs 把目录创建出来
        new File(destDir).mkdirs();
        //拼接 截图 名字
        String destFile = passfailMethod + "-"+ dateFormat.format(new Date()) + ".png";
        try {
            // scrFile 文件流
            FileUtils.copyFile(scrFile,new File(destDir + "/" + destFile));
            //做一个异常 的 处理
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    
}
